﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ResourceBlender_Express.Validators;
using System.Globalization;

namespace ResourceBlender_Express {
    /// <summary>
    /// A set of controls which allows editing of a translation, it's associated comment and performing actions such as searching and auto-translating
    /// </summary>
    public partial class TranslationInput : UserControl {
        public static readonly DependencyProperty TranslationProperty = DependencyProperty.Register("Translation", typeof(RBTranslation), typeof(TranslationInput));
        /// <summary>
        /// Gets or sets the translation being edited.
        /// </summary>
        /// <value>The translation.</value>
        public RBTranslation Translation {
            get { return base.GetValue(TranslationProperty) as RBTranslation; }
            set { 
                base.SetValue(TranslationProperty, value); 
            }
        }

        /// <summary>
        /// Initializes a new <see cref="TranslationInput"/>.
        /// </summary>
        public TranslationInput() {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new <see cref="TranslationInput"/> to edit the specified translation.
        /// </summary>
        /// <param name="translation">The translation to edit.</param>
        public TranslationInput(RBTranslation translation) {
            SetValue(TranslationProperty, translation);
            InitializeComponent();
            string englishName;
            try {
                englishName = CultureInfo.GetCultureInfo(translation.LanguageCode).EnglishName;
            } catch{
                englishName = translation.LanguageCode;
            }
            btnFindSimilar.ToolTip = string.Format("Search for similiar translations using the {0} translation", englishName);
            btnFindSimilar.CommandParameter = translation.LanguageCode;
        }

        /// <summary>
        /// Adds the binding rule to the Translation textbox.
        /// </summary>
        /// <param name="rule">The rule.</param>
        public void AddBindingRule(ValidationRule rule) {
            Binding bndTranslation = txtTranslation.GetBindingExpression(TextBox.TextProperty).ParentBinding;
            bndTranslation.ValidationRules.Add(rule);
        }

        /// <summary>
        /// Validates the input on the Translation textbox if a binding rule has been added.
        /// </summary>
        /// <returns></returns>
        public bool ValidateInput() {
            bool valid = true;
            Binding bnd = txtTranslation.GetBindingExpression(TextBox.TextProperty).ParentBinding;
            Window parentWindow = Window.GetWindow(this);
            foreach(ValidationRule rule in bnd.ValidationRules) {
                ValidationResult result = rule.Validate(txtTranslation.Text, CultureInfo.CurrentCulture);
                if(!result.IsValid) {
                    DialogBox.Show(parentWindow, result.ErrorContent.ToString(), "Error", DialogBox.DialogBoxType.OK, DialogBox.DialogBoxIcon.Error);
                    valid = false;
                }
            }
            if(!valid) {
                txtTranslation.Focus();
            }
            return valid;
        }

        /// <summary>
        /// Shows the extra controls in the expander.
        /// </summary>
        public void Expand() {
            EditControls.IsExpanded = true;
        }
    }
}
